package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.common.message.GetImageResponse;
import com.campus.common.message.ShopResponse;
import com.campus.common.message.UploadImageResponse;
import com.campus.common.type.BuyResult;
import com.campus.common.vo.Product;

import com.campus.server.service.ShopService;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import javax.swing.ImageIcon;


public class ShopController {
	private ClientConnector connector;
	
	public ShopController(ClientConnector connector) {

	    this.connector = connector;
	}

	public List<Product> getProducts() throws ClassNotFoundException, IOException {
        ShopResponse resp = connector.listProducts();
        return resp.getProducts();
    }

	public BuyResult buyProducts(List<Integer> productIds, List<Integer> quantities, String userId, String password) throws ClassNotFoundException, IOException {
	    ShopResponse resp = connector.buyProducts(productIds, quantities, userId, password);
	    return resp.getBuyResult();
	}
    
	public boolean addProduct(Product p, File imageFile) {
	    try {
	        String uploadedFileName = null;
	        if (imageFile != null) {
	            // 上传图片到服务器固定目录
	            uploadedFileName = connector.uploadImage(imageFile);
	            if (uploadedFileName == null) {
	                System.out.println("图片上传失败");
	                return false;
	            }
	        } else {
	            uploadedFileName = "default.png"; // 使用默认图片
	        }

	        // 商品信息只存图片文件名
	        p.setImageUrl(uploadedFileName);
	        ShopResponse resp = connector.addProduct(p);
	        return resp.isSuccess();
	    } catch (Exception e) {
	        e.printStackTrace();
	        return false;
	    }
	}

    public boolean deleteProduct(int id) {
        try {
            ShopResponse resp = connector.deleteProduct(id);
            return resp.isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateProduct(Product p) {
        try {
            ShopResponse resp = connector.updateProduct(p);
            return resp.isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateProductStatus(int id, int status) {
        try {
            ShopResponse resp = connector.updateProductStatus(id, status);
            return resp.isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean updateProductHot(int id, int hot) {
        try {
            ShopResponse resp = connector.updateProductStatus(id, hot);
            return resp.isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean updateProductNew(int id, int newP) {
        try {
            ShopResponse resp = connector.updateProductStatus(id, newP);
            return resp.isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean updateProductExpire(int id, int expire) {
        try {
            ShopResponse resp = connector.updateProductStatus(id, expire);
            return resp.isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public ImageIcon getProductImage(String fileName) {
        try {
            GetImageResponse resp = connector.getImage(fileName);
            if (resp.getImageData() != null) {
                return new ImageIcon(resp.getImageData());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null; // 图片不存在
    }
    
    public byte[] getProductImageBytes(String fileName) {
        try {
            GetImageResponse resp = connector.getImage(fileName);
            if (resp.getImageData() != null) {
                return resp.getImageData();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null; // 图片不存在
    }
    
    public String uploadImage(File imageFile) {
        try {
            if (imageFile == null) return null;
            byte[] data = Files.readAllBytes(imageFile.toPath());
            String fileName = imageFile.getName();
            UploadImageResponse resp = connector.uploadImage(fileName, data);
            return resp.isSuccess() ? fileName : null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



 // 可选：结束会话时关闭连接
    public void closeConnection() throws IOException {
        connector.close();
    }
}


